package com.recycler.coverflow.wcdb;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.wcdb.Cursor;
import com.tencent.wcdb.database.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class ErcDaoManager {
    private ErcDBHelper mHelper;
    private final SQLiteDatabase mDB;
    private static final String TAG = "zyj_db_m";
    public ErcDaoManager(Context context) {
        mHelper = new ErcDBHelper(context);
        mDB = mHelper.getReadableDatabase();
    }

    public int getCount(){
        SQLiteDatabase mWDb = mHelper.getReadableDatabase();
        Cursor cursor = mWDb.rawQuery(DB1.COUNT_TBL, null);
        int count = 0;
        if (cursor.moveToNext()) {
            count = cursor.getInt(0);
        }
        cursor.close();
        return count;
    }

    /**
     * 拿到数据库中所有的Person并放入集合中
     */
    public List<String> getMatchTagData(String TAG) {
        List<String> listData = new ArrayList<>();
        Cursor c = getMatchInfo(TAG);
        while (c.moveToNext()) {
            String value = c.getString(c.getColumnIndex(DB1.column_name));
            if(!TextUtils.isEmpty(value)){
                listData.add(value);
            }
        }
        c.close();
        return listData;
    }

    private Cursor getMatchInfo(String tag) {
        String sql = DB1.searchMathPro + "\'*" +tag+
                "*\'" + DB1.limited30;
        Log.v(TAG,"getLikeInfo ["+sql+"]");
        return mDB.rawQuery(sql, null);
    }

    /**
     * 拿到数据库中所有的Person并放入集合中
     */
    public List<String> getLikeTagData(String TAG) {
        List<String> listData = new ArrayList<>();
        Cursor c = getLikeInfo(TAG);
        while (c.moveToNext()) {
            String value = c.getString(c.getColumnIndex(DB1.column_name));
            if(!TextUtils.isEmpty(value)){
                listData.add(value);
            }
        }
        c.close();
        return listData;
    }

    private Cursor getLikeInfo(String tag) {
        String sql = DB1.searchLikePro + "\'%" +tag+
                "%\'" + DB1.limited30;
        Log.v(TAG,"getLikeInfo ["+sql+"]");
        return mDB.rawQuery(sql, null);
    }

    /**
     * 关闭  database；
     */
    public void closeDB() {
        mDB.close();
    }
}
